using System;
using System.Windows.Forms;
using FBroSharp.Lib;
using FBroSharp.Event;
using FBroSharp.DataType;
using FBroSharp.Value;
using FBroSharp.Const;

namespace BackgroundMouseAutomation
{
    /// <summary>
    /// 浏览器事件处理类 - 处理浏览器生命周期事件
    /// </summary>
    public class BrowserEvent : FBroSharpBrowserEvent
    {
        private Form1 _form;

        public BrowserEvent(Form1 form)
        {
            _form = form;
        }

        /// <summary>
        /// 浏览器创建完成后触发
        /// </summary>
        public override void OnAfterCreated(IFBroSharpBrowser browser, IFBroSharpDictionaryValue extrainfo)
        {
            _form.UpdateStatus("✅ 浏览器已创建，等待页面加载...");
            Console.WriteLine($"浏览器创建成功 - ID: {browser.GetIdentifier()}");
        }

        /// <summary>
        /// 地址栏变化时触发
        /// </summary>
        public override void OnAddressChange(IFBroSharpBrowser browser, IFBroSharpFrame frame, string url)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus($"🌐 正在访问: {url}");
            }
        }

        /// <summary>
        /// 页面标题变化时触发
        /// </summary>
        public override void OnTitleChange(IFBroSharpBrowser browser, string title)
        {
            _form.UpdateFormTitle(title);
        }

        /// <summary>
        /// 页面加载状态变化时触发
        /// </summary>
        public override void OnLoadingStateChange(IFBroSharpBrowser browser, bool isLoading, bool canGoBack, bool canGoForward)
        {
            if (!isLoading)
            {
                _form.UpdateStatus("✅ 页面加载完成，可以开始拖动滑块");
                _form.EnableSliderButtons();
            }
        }

        /// <summary>
        /// 页面加载完成时触发
        /// </summary>
        public override void OnLoadEnd(IFBroSharpBrowser browser, IFBroSharpFrame frame, int httpStatusCode)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus($"✅ 页面加载完成 (HTTP {httpStatusCode})");
                Console.WriteLine($"页面加载完成 - URL: {frame.GetURL()}");
            }
        }

        /// <summary>
        /// 页面加载错误时触发
        /// </summary>
        public override void OnLoadError(IFBroSharpBrowser browser, IFBroSharpFrame frame, 
            int errorCode, string errorText, string failedUrl)
        {
            if (frame.IsMain())
            {
                _form.UpdateStatus($"❌ 加载失败: {errorText}");
                Console.WriteLine($"加载错误 - Code: {errorCode}, URL: {failedUrl}");
            }
        }

        /// <summary>
        /// 阻止弹窗 - 所有链接在当前窗口打开
        /// </summary>
        public override bool OnBeforePopup(
            IFBroSharpBrowser browser, 
            IFBroSharpFrame frame, 
            string target_url, 
            string target_frame_name,
            FBroSharpWindowOpenDisposition target_disposition, 
            bool user_gesture, 
            FBroSharpPopupfeatures popupFeatures, 
            ref FBroSharpWindowsInfo windowInfo,
            ref FBroSharpBrowserSetting settings,
            ref bool no_javascript_access, 
            IFBroSharpUseExtraData user_settings)
        {
            // 在当前窗口打开
            frame.LoadURL(target_url);
            return true; // 阻止弹窗
        }

        /// <summary>
        /// 浏览器关闭前触发
        /// </summary>
        public override void OnBeforeClose(IFBroSharpBrowser browser)
        {
            _form.UpdateStatus("🔒 浏览器已关闭");
            Console.WriteLine("浏览器已关闭");
        }

        /// <summary>
        /// 询问是否可以关闭浏览器
        /// </summary>
        public override bool DoClose(IFBroSharpBrowser browser)
        {
            return false; // false = 允许关闭
        }
    }
}

